package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.CircleCollectAdapter;
import com.dingwei.lesyndrome.adapter.GamCircleListAdapter;
import com.dingwei.lesyndrome.adapter.NewsCollectAdapter;
import com.dingwei.lesyndrome.adapter.PeripheralCollectAdapter;
import com.dingwei.lesyndrome.bean.CollectBean;
import com.dingwei.lesyndrome.bean.GamCircleListBean;
import com.dingwei.lesyndrome.mvp.presenter.CollectPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.nikhilpanju.recyclerviewenhanced.RecyclerTouchListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/3/7.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class CollectActivity extends BaseActivity implements Contract.CollectView {
    CollectPresenter collectPresenter = new CollectPresenter(this);

    @Bind(R.id.news_tab_layout)
    TabLayout newsTabLayout;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.data_layout)
    RelativeLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    PeripheralCollectAdapter peripheralCollectAdapter;
    NewsCollectAdapter newsCollectAdapter;
    CircleCollectAdapter circleCollectAdapter;
    private RecyclerTouchListener onTouchListener;
    private String type = "1";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_collect;
    }

    @Override
    protected void initData() {
        page = 1;
        collectPresenter.initData(page, type, true, true);
    }

    @Override
    protected void initView() {
        newsTabLayout.addTab(newsTabLayout.newTab().setText("服务"));
        newsTabLayout.addTab(newsTabLayout.newTab().setText("动态"));
        newsTabLayout.addTab(newsTabLayout.newTab().setText("圈子"));


        peripheralCollectAdapter = new PeripheralCollectAdapter(BaseActivity.context);
        newsCollectAdapter = new NewsCollectAdapter(BaseActivity.context);
        circleCollectAdapter = new CircleCollectAdapter(BaseActivity.context);

        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.parting_line), 1));
        mRecyclerView.setNestedScrollingEnabled(false);

        mRecyclerView.setAdapter(peripheralCollectAdapter);


        newsTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                peripheralCollectAdapter.clear();
                newsCollectAdapter.clear();
                circleCollectAdapter.clear();
                if (tab.getPosition() == 0) {
                    mRecyclerView.setAdapter(peripheralCollectAdapter);
                    type = "1";
                } else if (tab.getPosition() == 1) {
                    mRecyclerView.setAdapter(newsCollectAdapter);
                    type = "2";
                } else {
                    mRecyclerView.setAdapter(circleCollectAdapter);
                    type = "3";
                }
                page = 1;
                collectPresenter.initData(page, type, true, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });



        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                collectPresenter.initData(page, type, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                collectPresenter.initData(page, type, true, false);
            }
        });


        onTouchListener = new RecyclerTouchListener(BaseActivity.context, mRecyclerView);
        onTouchListener.setClickable(new RecyclerTouchListener.OnRowClickListener() {
            @Override
            public void onRowClicked(int position) {
//                        showToast("Row " + (position + 1) + " clicked!");
                switch (type){
                    case "1":
                        CollectBean.DataBean bean = peripheralCollectAdapter.getItem(position);
                        intent  = new Intent(BaseActivity.context, PeripheralDetailActivity.class);
                        intent.putExtra("id",bean.object_id);
                        startActivity(intent);

                        break;
                    case "2":
                        CollectBean.DataBean bean1 = newsCollectAdapter.getItem(position);
                        intent  = new Intent(BaseActivity.context, NewDetailsActivity.class);
                        intent.putExtra("id",bean1.object_id);
                        startActivity(intent);
                        break;
                    case "3":
                        CollectBean.DataBean bean2 = circleCollectAdapter.getItem(position);
                        intent  = new Intent(BaseActivity.context, CircleDetailsActivity.class);
                        intent.putExtra("id",bean2.object_id);
                        startActivity(intent);
                        break;
                }

            }

            @Override
            public void onIndependentViewClicked(int independentViewID, int position) {
//                        showToast("Button in row " + (position + 1) + " clicked!");
            }
        })
                .setSwipeOptionViews(R.id.tv_delete)
                .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                    @Override
                    public void onSwipeOptionClicked(int viewID, int position) {
                        if (viewID == R.id.tv_delete) {
                            CollectBean.DataBean bean = null;
                            switch (type){
                                case "1":
                                    bean = peripheralCollectAdapter.getItem(position);
                                    break;
                                case "2":
                                    bean = newsCollectAdapter.getItem(position);
                                    break;
                                case "3":
                                    bean = circleCollectAdapter.getItem(position);
                                    break;
                            }
                            collectPresenter.delete(bean.object_id,type,position);
                        }
                    }
                });

        circleCollectAdapter.xxInterfaceListener(new GamCircleListAdapter.clickInterFace() {
            @Override
            public void onLike(int position) {
                CollectBean.DataBean bean = circleCollectAdapter.getItem(position);
                collectPresenter.like(bean.object_id,position);
            }

            @Override
            public void onComment(int position) {

            }

            @Override
            public void onPlay(int position) {
                CollectBean.DataBean bean  = circleCollectAdapter.getItem(position);
                Intent intent = new Intent(BaseActivity.context, VideoPlayerActivity.class);
                intent.putExtra("url", bean.video);
                intent.putExtra("face",  bean.vimg);
                startActivity(intent);
            }

            @Override
            public void onHead(int position) {
                CollectBean.DataBean bean  = circleCollectAdapter.getItem(position);
                Intent intent = new Intent(BaseActivity.context, HisCircleActivity.class);
                intent.putExtra("mid",bean.member_id);
                startActivity(intent);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        mRecyclerView.addOnItemTouchListener(onTouchListener);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mRecyclerView.removeOnItemTouchListener(onTouchListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onFail() {
        showData("fail");
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            switch (type) {
                case "1":
                    if (peripheralCollectAdapter.getAllData() != null && peripheralCollectAdapter.getAllData().size() > 0) {
                        refreshLayout.setVisibility(View.VISIBLE);
                        noDataLayout.setVisibility(View.GONE);
                    } else {
                        refreshLayout.setVisibility(View.GONE);
                        noDataLayout.setVisibility(View.VISIBLE);
                    }
                    break;
                case "2":
                    if (newsCollectAdapter.getAllData() != null && newsCollectAdapter.getAllData().size() > 0) {
                        refreshLayout.setVisibility(View.VISIBLE);
                        noDataLayout.setVisibility(View.GONE);
                    } else {
                        refreshLayout.setVisibility(View.GONE);
                        noDataLayout.setVisibility(View.VISIBLE);
                    }
                    break;
                case "3":
                    if (circleCollectAdapter.getAllData() != null && circleCollectAdapter.getAllData().size() > 0) {
                        refreshLayout.setVisibility(View.VISIBLE);
                        noDataLayout.setVisibility(View.GONE);
                    } else {
                        refreshLayout.setVisibility(View.GONE);
                        noDataLayout.setVisibility(View.VISIBLE);
                    }
                    break;
            }

        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onRefresh(CollectBean bean) {
        switch (type) {
            case "1":
                peripheralCollectAdapter.clear();
                peripheralCollectAdapter.addAll(bean.data);
                refreshLayout.finishRefresh();
                showData("success");
                break;
            case "2":
                newsCollectAdapter.clear();
                newsCollectAdapter.addAll(bean.data);
                refreshLayout.finishRefresh();
                showData("success");
                break;
            case "3":
                circleCollectAdapter.clear();
                circleCollectAdapter.addAll(bean.data);
                refreshLayout.finishRefresh();
                showData("success");
                break;
        }
    }

    @Override
    public void onLoadMore(CollectBean bean) {
        switch (type) {
            case "1":
                peripheralCollectAdapter.addAll(bean.data);
                refreshLayout.finishLoadmore();
                showData("success");
                break;
            case "2":
                newsCollectAdapter.addAll(bean.data);
                refreshLayout.finishLoadmore();
                showData("success");
                break;
            case "3":
                circleCollectAdapter.addAll(bean.data);
                refreshLayout.finishLoadmore();
                showData("success");
                break;
        }
    }

    @Override
    public void likeSuccess(int positon) {
        CollectBean.DataBean bean = circleCollectAdapter.getItem(positon);
        bean.isAgree = !bean.isAgree;
        circleCollectAdapter.notifyItemChanged(positon,bean);
    }

    @Override
    public void collectSuccess(int position) {
        switch (type){
            case "1":
                peripheralCollectAdapter.remove(position);
                break;
            case "2":
                newsCollectAdapter.remove(position);
                break;
            case "3":
                circleCollectAdapter.remove(position);
                break;
        }
        showData("success");
    }


}
